import socket
import re

def main():
    try:
        # 创建TCP连接的Socket对象
        tcp_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

        # 创建UDP连接的Socket对象
        udp_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

        # 连接到同花顺App服务器
        host = '119.147.212.81'  # 替换为同花顺App服务器的地址
        port = 7709  # 替换为同花顺App服务器的端口
        tcp_socket.connect((host, port))
        # 发送数据
        data = 'hello'
        tcp_socket.send(data.encode())

        # 接收数据包
        recv_data = tcp_socket.recv(1024)
        print(recv_data.decode())

        # 使用正则表达式提取字段
        '''field_pattern = r'Field: (\w+)'
        fields = re.findall(field_pattern, data)'''

        # 打印提取到的字段
        '''for field in fields:
            print(field)'''

        # 关闭套接字
        #tcp_socket.close()
    except socket.error as e:
        print(f'Socket error: {e}')
    except socket.timeout as e:
        print(f'Socket timeout: {e}')


if __name__ == '__main__':
    main()